//
//  LLNetwordTools.swift
//  xinlangweibo
//
//  Created by 任玉林 on 16/6/26.
//  Copyright © 2016年 任玉林. All rights reserved.
//

import UIKit

import AFNetworking

//给成功失败的闭包起个别名，为了后边使用方便
typealias SuccessClosure = (response: AnyObject?) -> ()
typealias FailureClosure = (error: NSError) -> ()


//请求数据枚举
enum MethodType: String{
    
    case GET = "GET"
    case POST = "POST"
}

class LLNetwordTools: AFHTTPSessionManager {
    
     static let sharedtools: LLNetwordTools = {
        
        let tools = LLNetwordTools()
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    
    
    //请求网络数据
    
    func requst(method: MethodType, urlstring: String, parameters: AnyObject?, success: SuccessClosure, failure: FailureClosure){
        
        if method == .GET {
            
            self.GET(urlstring, parameters: parameters, progress: nil , success: { (_, res) -> Void in
                
                success(response: res)
                
                }, failure: { (_, err) -> Void in
                    
                    failure(error: err)
                    
            })
        }else{
            
            self.POST(urlstring, parameters: parameters, progress: nil , success: { (_, res) -> Void in
                
                success(response: res)
                
                }, failure: { (_, err) -> Void in
                    
                    failure(error: err)
         })
            
      }

   }
    
}

extension LLNetwordTools{
    
    //请求token
    func oauthLoadUserAccont(code:String,success:SuccessClosure, failure: FailureClosure){
    
         let url = "https://api.weibo.com/oauth2/access_token"
        
        let param = [
            
            "client_id":APPKEY,
            "client_secret":APPSECRET,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":"http://www.baidu.com"
            
        ]
        self.requst(MethodType.POST, urlstring: url, parameters: param, success: success, failure: failure)
        
    }
    
    //请求个人信息
    func oauthLoadUserInfo(userAccount:LLUserAccount,success:SuccessClosure, failure: FailureClosure){
        
        let url = "https://api.weibo.com/2/users/show.json"
        
        let param = [
            
            "access_token":userAccount.access_token!,
            
            "uid":userAccount.uid!
        
        ]
        
        self.requst(MethodType.GET, urlstring: url, parameters: param, success: success, failure: failure)
        
    }
    
    //请求首页数据
    func homeLoadData(sinceId:Int64, maxId: Int64,success:SuccessClosure,failure:FailureClosure){
        
        let urlStr = "https://api.weibo.com/2/statuses/friends_timeline.json"
        
        let param = [
            
            "access_token":LLUserAccountTools.sharedTools.accountToken!,
            
            "since_id":"\(sinceId)",
            "max_id":"\(maxId)"
        ]
        
        self.requst(MethodType.GET, urlstring: urlStr, parameters: param, success: success, failure: failure)
    }
    
    //发送微博信息
    func composeUpData(statas: String,success:SuccessClosure,failure:FailureClosure){
        
        let urlStr = "https://api.weibo.com/2/statuses/update.json"
        
       let  parmar = [
        
        "access_token": LLUserAccountTools.sharedTools.accountToken!,
        "status": statas
        ]
        self.requst(MethodType.POST, urlstring: urlStr, parameters: parmar, success: success, failure: failure)
        
        
    }
    
    //发送有配图微博信息
    func composeUpData(statas: String,imageList:[UIImage], success: (response: AnyObject?)->(), failure:(error: NSError)->() ){
        
        // 请求url
        let URLString = "https://upload.api.weibo.com/2/statuses/upload.json"
        // 请求参数
        let params = [
            "access_token":LLUserAccountTools.sharedTools.accountToken!,
            "status":statas
            
        ]
        
        POST(URLString, parameters: params, constructingBodyWithBlock: { (formdata) -> Void in
            
            for image in imageList{
                
                let data = UIImagePNGRepresentation(image)!
                
                formdata.appendPartWithFileData(data, name: "pic", fileName: "oooo", mimeType: "application/octet-stream")
            }
            
            }, progress: nil, success: { (_, response) -> Void in
                
                success(response: response)

            }){ (_, error) -> Void in
                failure(error: error)
        }
        
    }

    
}
